package com.freen.fadmin.controller.jwt.SysuserApi.sysuser;

import com.freen.fadmin.controller.jwt.JwtSysuserBaseController;
import com.freen.fadmin.entity.sysuser.SysPermission;
import com.freen.fadmin.entity.sysuser.SysRole;
import com.freen.fadmin.repository.sysuser.SysPermissionRepository;
import com.freen.fadmin.repository.sysuser.SysRoleRepository;
import com.freen.fadmin.repository.sysuser.SysUserRepository;
import com.freen.fadmin.util.result.ExceptionMsg;
import com.freen.fadmin.util.result.Response;
import com.freen.fadmin.util.result.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/SysApi")
public class SysPermissionController extends JwtSysuserBaseController{
    @Autowired
    HttpServletRequest request;
    @Autowired
    private SysUserRepository sysUserRepository;
    @Autowired
    private SysRoleRepository sysRoleRepository;
    @Autowired
    private SysPermissionRepository sysPermissionRepository;




    //添加权限
    @ResponseBody
    @RequestMapping(value = "/sysPermission",method = RequestMethod.POST)
    public Response PermissionAdd(SysPermission sysPermission, String role){
        try {
            SysPermission sysPermission1 = sysPermissionRepository.findByName(sysPermission.getName());
            if (null != sysPermission1){
                return result(ExceptionMsg.ParamDuplication);
            }
            List<SysRole> roles = new ArrayList<>();
            SysRole role1 = sysRoleRepository.findByRole(role);
            roles.add(role1);
            sysPermission.setName(sysPermission.getName());
            sysPermissionRepository.save(sysPermission);
        }catch (Exception e){
            System.out.println(e.toString());
            return result(ExceptionMsg.FAILED);
        }
        return result();
    }


    //权限路由
    //restful控制器编写
    //index
    //create 前后端分离后，就暂时不用了。
    //store
    //show 前后端分离后，就暂时不用了
    //edit 前后端分离后，就暂时不用了
    //update id put
    //destroy id delete

    @ResponseBody
    @RequestMapping(value = "/sysPermission",method = RequestMethod.GET)
    public Response APIPermissionindex(@RequestParam(value = "page", defaultValue = "0") Integer start,
                                       @RequestParam(value = "limit", defaultValue = "5") Integer limit,
                                       @RequestParam(value = "keyword",required = false) String  keyword,
                                       @RequestParam(value = "sort", defaultValue = "id") String  sortq){
        start = start < 0 ? 0 : start;
        Sort sort = Sort.by(Sort.Direction.DESC, sortq);
        Pageable pageable = PageRequest.of(start-1,limit,sort);
        //得到了初始的数据
        Page<SysPermission> page;
        //判断是否有 keyword 如果有就找，如果没有就查全部
        if (null != keyword && !"".equals(keyword)){
            //page = sysUserRepository.findByNameLikeOrPhoneLikeOrCnnameLike(keyword,keyword,keyword,pageable);
            //page = sysUserRepository.findByNameLike("%"+keyword+"%",pageable);
            page = sysPermissionRepository.findByNameLikeOrCnnameLike("%"+keyword+"%","%"+keyword+"%",pageable);

        }else{
            page = sysPermissionRepository.findAll(pageable);
        }
        //在没有新知识补充之前，看来只能用hashmap去构造返回的数据了。
        //先构造返回的数据结构
        return new ResponseData(ExceptionMsg.SUCCESS,page);
    }

    @ResponseBody
    @RequestMapping(value = "/sysPermission/create",method = RequestMethod.POST)
    public Response APIPermissionstore(@RequestBody Map<String ,Object> params){
        SysPermission sysPermission = new SysPermission();
        sysPermission.setName(params.get("name").toString());
        sysPermission.setCnname(params.get("cnname").toString());
        sysPermission.setRemark(params.get("remark").toString());
        sysPermission.setAvailable((Boolean) params.get("available"));
        sysPermissionRepository.save(sysPermission);
        return new ResponseData(ExceptionMsg.SUCCESS,sysPermission);
    }

    @ResponseBody
    @RequestMapping(value = "/sysPermission/{id}",method = RequestMethod.PUT)
    public Response APIPermissionupdate(@PathVariable("id")int id,@RequestBody Map<String ,Object> params){
        SysPermission sysPermission = sysPermissionRepository.findById(id);
        sysPermission.setName(params.get("name").toString());
        sysPermission.setCnname(params.get("cnname").toString());
        sysPermission.setRemark(params.get("remark").toString());
        sysPermission.setAvailable((Boolean) params.get("available"));
        sysPermissionRepository.save(sysPermission);
        return new ResponseData(ExceptionMsg.SUCCESS,sysPermission);
    }



    //冻结权限
    @ResponseBody
    @RequestMapping(value = "/sysPermission/Freeze/{id}",method = RequestMethod.POST)
    public Response APIPermissionFreeze(@PathVariable("id")int id){
        //取出用户
        SysPermission sysPermission = sysPermissionRepository.findById(id);
        sysPermission.setAvailable(false);
        sysPermissionRepository.save(sysPermission);
        return result(ExceptionMsg.SUCCESS);
    }

    @ResponseBody
    @RequestMapping(value = "/sysPermission/{id}",method = RequestMethod.DELETE)
    public Response APIPermissiondestroy(@PathVariable("id")int id){
        sysPermissionRepository.deleteById(id);
        return result(ExceptionMsg.SUCCESS);
    }

    //权限总不分页
    @ResponseBody
    @RequestMapping(value = "/sysPermissionAll",method = RequestMethod.GET)
    public Response APIsysPermissionAll(){
        //获取所有角色数据
        List<SysPermission> sysPermissions = sysPermissionRepository.findAll();
        return new ResponseData(ExceptionMsg.SUCCESS,sysPermissions);
    }

}
